package zhai.jvm;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class BlowUpJVM {

    public static void main(String[] args) {
        System.out.println(100<<1);
    }

    //栈深度溢出
    public static void  testStackOverFlow(){
        BlowUpJVM.testStackOverFlow();
    }

    //不能引起永久代溢出
    public static void testPergemOutOfMemory1(){
        //方法一失败
        List<String> list = new ArrayList<String>();
        while(true){
            list.add(UUID.randomUUID().toString().intern());
        }
    }


    //JVM内存溢出
    public static void testStackOutOfMemory(){
        while (true) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true){
                    }
                }
            });
            thread.start();
        }
    }

    //堆溢出
    public static void testOutOfHeapMemory(){
        List<StringBuffer> list = new ArrayList<StringBuffer>();
        while(true){
            StringBuffer B = new StringBuffer();
            for(int i = 0 ; i < 10000 ; i++){
                B.append(i);
            }
            list.add(B);
        }
    }
}